package com.mdd.admin.controller;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IDifferentiateCategoryService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.DifferentiateCategoryCreateValidate;
import com.mdd.admin.validate.DifferentiateCategoryUpdateValidate;
import com.mdd.admin.validate.DifferentiateCategorySearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.commons.StringIdValidate;
import com.mdd.admin.vo.DifferentiateCategoryListedVo;
import com.mdd.admin.vo.DifferentiateCategoryDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/category")
@Api(tags = "产品分类管理")
public class DifferentiateCategoryController {

    @Resource
    IDifferentiateCategoryService iDifferentiateCategoryService;

    @GetMapping("/list")
    @ApiOperation(value="产品分类列表")
    public AjaxResult<PageResult<DifferentiateCategoryListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated DifferentiateCategorySearchValidate searchValidate) {
        PageResult<DifferentiateCategoryListedVo> list = iDifferentiateCategoryService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="产品分类详情")
    public AjaxResult<DifferentiateCategoryDetailVo> detail(@Validated @IDMust() @RequestParam("id") String id) {
        DifferentiateCategoryDetailVo detail = iDifferentiateCategoryService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "产品分类新增")
    @PostMapping("/add")
    @ApiOperation(value="产品分类新增")
    public AjaxResult<Object> add(@Validated @RequestBody DifferentiateCategoryCreateValidate createValidate) {
        iDifferentiateCategoryService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "产品分类编辑")
    @PostMapping("/edit")
    @ApiOperation(value="产品分类编辑")
    public AjaxResult<Object> edit( @RequestBody DifferentiateCategoryUpdateValidate updateValidate) {
        iDifferentiateCategoryService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "产品分类删除")
    @PostMapping("/del")
    @ApiOperation(value="产品分类删除")
    public AjaxResult<Object> del( @RequestBody StringIdValidate idValidate) {
        iDifferentiateCategoryService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
